package com.snopy.concurrency.countdownlatch;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoCountDownLatch
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/30 12:29
 */
@Slf4j
public class DemoCountDownLatch {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        new Thread(()->{
            log.debug("waiting...");
            try {
                countDownLatch.await();

                log.debug("end wait...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->{

            try {
                TimeUnit.SECONDS.sleep(1);
                log.debug("countDown...");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"t2").start();
        new Thread(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(1500);
                log.debug("countDown...");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t3").start();
        new Thread(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(1500);
                log.debug("countDown...");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t4").start();
        new Thread(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(1500);
                log.debug("countDown...");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t5").start();
        new Thread(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(3000);
                log.debug("countDown...");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t6").start();
    }
}
